<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px" class="queryForms">
			<el-form-item label="合同名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入合同名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="批次" prop="batch">
				<el-input v-model="queryParams.batch" placeholder="请输入批次" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<!-- <el-form-item label="状态" prop="applyTime">
				<el-select v-model="queryParams.status" placeholder="请选择">
					<el-option v-for="dict in dict.type.contract_status" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['finance:contract:create']">新增合同</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['finance:contract:remove']">删除</el-button>
			</el-col>

			<CreateForm :ids="ids" formtype="t_contract" />
			<right-toolbar @changeTableData="changeTableData" menuNames="contract18" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="finance:contract:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="auxiliaryConsumablesTypeList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="合同名称" prop="contractName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="合同号" prop="contractNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="客户名称" prop="clientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="合同金额" prop="contractAmount" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<!-- <el-table-column label="执行状态" align="center" prop="status" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.contract_status" :value="scope.row.status" />
					</template>
				</el-table-column> -->
				<el-table-column label="批次" prop="batch" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="套数" prop="quantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="不含税金额" prop="priceExcludingTax" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" />
				<el-table-column label="税额" prop="tax" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible" />
				<el-table-column label="价税合计" prop="total" min-width="150" :show-overflow-tooltip="true" v-if="columns[8].visible" />

				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="180">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['finance:contract:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleJump(scope.row)" v-hasPermi="['finance:contract:annex']">附件</el-button>

						<el-dropdown size="mini" @command="command => handleCommand(command, scope.row)" v-hasPermi="['finance:contract:invoice', 'finance:contract:download']">
							<el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
							<el-dropdown-menu slot="dropdown">
								<el-dropdown-item command="handleJump2" v-hasPermi="['finance:contract:invoice']">发票</el-dropdown-item>
								<!-- <el-dropdown-item command="handleDownload" v-hasPermi="['finance:contract:download']">下载</el-dropdown-item> -->
							</el-dropdown-menu>
						</el-dropdown>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination21" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或合同对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form2" :model="form" :rules="rules" label-width="100px">
				<el-form-item label="合同名称" prop="contractName">
					<el-input v-model="form.contractName" placeholder="请输入合同名称" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="合同号" prop="contractNumber">
					<el-input v-model="form.contractNumber" placeholder="请输入合同号" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="客户" prop="clientId">
					<el-select v-model="form.clientId" placeholder="请选择" @change="selectClient">
						<el-option v-for="dict in dict.type.default_cus" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="合同金额" prop="contractAmount">
					<el-input-number v-model="form.contractAmount" placeholder="请输入合同金额" :min="0" :max="99999999" :precision="2" />
				</el-form-item>
				<el-form-item label="结算名称" prop="batch">
					<el-select v-model="form.settleAccountsId" @change="selectsettleAccounts">
						<el-option v-for="item in List1" :key="item.id" :label="item.settleAccountsName" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="批次" prop="batch">
					<el-input v-model="form.batch" placeholder="请输入批次" maxlength="20" show-word-limit  disabled />
				</el-form-item>
				<el-form-item label="套数" prop="quantity">
					<el-input-number v-model="form.quantity" placeholder="请输入套数" :min="1" :max="99999999" disabled />
				</el-form-item>
				<el-form-item label="不含税金额" prop="priceExcludingTax">
					<el-input-number v-model="form.priceExcludingTax" placeholder="请输入不含税金额" :min="0" :max="99999999" :precision="2" />
				</el-form-item>
				<el-form-item label="税额" prop="tax">
					<el-input-number v-model="form.tax" placeholder="请输入税额" :min="0" :max="99999999" :precision="2" />
				</el-form-item>
				<el-form-item label="价税合计" prop="total">
					<el-input-number v-model="form.total" placeholder="请输入价税合计" :min="0" :max="99999999" :precision="2" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { AccountsList1, listContract, getContract, delContract, addContract, updateContract } from "@/api/finance/contract"
import { clientList,getcolumn } from "@/api/selectList"
import { mapGetters } from "vuex"
import CreateForm from "@/views/components/CreateForm"
export default {
	name: "Contract",
	dicts: ["contract_status", "default_cus"],
	components: { CreateForm },
	data() {
		return {
			List1: [],
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 合同表格数据
			auxiliaryConsumablesTypeList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				name: undefined,
				state: undefined,
				batch: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				contractName: [{ required: true, message: "请输入合同名称", trigger: "blur" }],
				contractNumber: [{ required: true, message: "请输入合同编号", trigger: "blur" }],
				batch: [{ required: true, message: "请输入批次", trigger: "blur" }],
				quantity: [{ required: true, message: "请输入套数", trigger: "blur" }],
				priceExcludingTax: [{ required: true, message: "请输入不含税金额", trigger: "blur" }],
				tax: [{ required: true, message: "请输入税额", trigger: "blur" }],
				total: [{ required: true, message: "请输入价税合计", trigger: "blur" }],
				clientId: [{ required: true, message: "请输入客户", trigger: "blur" }],
				contractAmount: [{ required: true, message: "请输入合同金额", trigger: "blur" }]
			},
			clientDatas: [],
			// 列信息
			columns: [
				{ key: 0, label: `合同名称`, visible: true },
				{ key: 1, label: `合同号`, visible: true },
				{ key: 2, label: `客户名称`, visible: true },
				{ key: 3, label: `合同金额`, visible: true },
				// { key: 4, label: `执行状态`, visible: true },
				{ key: 4, label: `批次`, visible: true },
				{ key: 5, label: `套数`, visible: true },
				{ key: 6, label: `不含税金额`, visible: true },
				{ key: 7, label: `税额`, visible: true },
				{ key: 8, label: `价税合计`, visible: true }
			]
		}
	},
	computed: {
		...mapGetters(["sidebar"])
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getclientList()
		this.getSize()
		this.AccountsList()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	created() {
		this.getclientList()
		this.getSize()
		this.AccountsList()
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination21" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		selectClient(e) {
			this.dict.type.default_cus.forEach(item => {
				if (e == item.value) {
					this.form.clientName = item.label
				}
			})
		},
		selectsettleAccounts(e) {
			this.List1.forEach(element => {
				if (element.id == e) {
					this.form.batch = element.batch
					this.form.quantity = element.quantity
				}
			})
		},
		AccountsList() {
			AccountsList1().then(res => {
				this.List1 = res.rows
			})
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询合同列表 */
		getList() {
			this.loading = true
			listContract(this.queryParams).then(response => {
				this.auxiliaryConsumablesTypeList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 用户列表
		getclientList() {
			clientList({
				clientState: "1"
			}).then(res => {
				this.clientDatas = res.rows
			})
		},
		changeClient() {
			let cur = this.clientDatas.find(item => {
				return item.id == this.form.clientId
			})
			this.form.clientName = cur.clientName
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				contractName: undefined,
				contractNumber: undefined,
				clientId: undefined,
				clientName: undefined,
				contractAmount: undefined,
				repairNumber: undefined,
				batch: undefined,
				quantity: undefined,
				priceExcludingTax: undefined,
				tax: undefined,
				total: undefined
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		// 更多操作触发
		handleCommand(command, row) {
			this[command](row)
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加合同"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getContract(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "合同"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != undefined) {
						updateContract(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addContract(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delContract(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},

		// 更改状态
		changeStauts(row) {
			let data = {
				id: row.id,
				state: Number(row.state)
			}
			this.$modal
				.confirm("确认要修改状态吗？")
				.then(function () {
					return updateContract(data)
				})
				.then(() => {
					this.$modal.msgSuccess("操作成功")
				})
				.catch(() => {
					row.state = row.state == "0" ? "1" : "0"
				})
		},
		// 跳转到附件
		handleJump(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 21,
					perm: "finance:contract:annex"
				}
			})
		},
		// 跳转到发票列表
		handleJump2(row) {
			let url = "/finance/contractInvoice?id=" + row.id
			this.$router.push(url)
		},
		// 下载
		handleDownload() {
			this.$modal.msgSuccess("合同模板下载")
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 180px !important;
}
</style>
